package com.mindainfo.groundwave.server.member.dao.impl;

import com.mindainfo.groundwave.server.base.dao.impl.BaseDaoImpl;
import com.mindainfo.groundwave.server.member.bean.SysPermission;
import com.mindainfo.groundwave.server.member.dao.IPermissionDao;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;

/**
 * Created by MD-01 on 2017/5/15.
 */
@Repository
public class PermissionDaoImpl extends BaseDaoImpl<SysPermission> implements IPermissionDao {
    @Override
    public List<SysPermission> getByIds(Collection<Integer> permissionIds) {
        return getDatastore().createQuery(SysPermission.class)
                .field("id").in(permissionIds)
                .order("orderNum")
                .asList();
    }

    public List<SysPermission> queryList(int start,int num,String name,Integer type){
        Query<SysPermission> query = getDatastore().createQuery(SysPermission.class);
        if(!StringUtils.isEmpty(name)){
            query.field("name").contains(name);
        }
        if(type!=null){
            query.field("type").equal(type);
        }
        FindOptions findOptions = new FindOptions();
        findOptions.skip(start).limit(num);
        return query.order("type,parentId,orderNum").asList(findOptions);
    }

    public int queryListCount(String name,Integer type){
        Query<SysPermission> query = getDatastore().createQuery(SysPermission.class);
        if(!StringUtils.isEmpty(name)){
            query.field("name").contains(name);
        }
        if(type!=null){
            query.field("type").equal(type);
        }
        return (int)query.count();
    }

    @Override
    public SysPermission queryByName(String name) {
        return getDatastore().createQuery(SysPermission.class)
                .field("name").equal(name)
                .get();
    }

    @Override
    public SysPermission queryBySign(String sign) {
        return getDatastore().createQuery(SysPermission.class)
                .field("sign").equal(sign)
                .get();
    }

    @Override
    public List<SysPermission> queryAll() {

        return getDatastore().createQuery(SysPermission.class)
                .asList();
    }

    /**
     * 查询所有可用的权限
     * @return
     */
    public List<SysPermission> queryUsableAll(){
        return getDatastore().createQuery(SysPermission.class)
                .field("usable").equal(true)
                .asList();
    }
}
